<?php
/**
 * Created by PhpStorm.
 * User: Dick
 * Date: 2016/4/14
 * Time: 1:01
 */

namespace Model;

use Method\JSON;

class UserInfo
{
    public $id;
    public $nickname;
    public $username;
    public $avatar;
    public $background;
    public $lasttime;
    public $personal;
    public $moodnum;
    public $following;
    public $followed;


    public static function GetUserInfo($username)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statament = $pdo->prepare('SELECT id,nickname,username,avatar,bg,personal,lasttime FROM user WHERE username=?');
        $statament->bindValue(1, $username);
        $statament->execute();
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $result = $statament->fetchAll();
        $result[0]['following'] = self::GetUserfollowing($result[0]['id']);
        $result[0]['followed'] = self::GetUserfollowed($result[0]['id']);
        $result[0]['moodnum'] = self::GetUsermoodnum($result[0]['id']);
        $result[0]['moodsc'] = self::GetUsermoodnum($result[0]['id']);
        if (!$inTransaction) {
            $pdo->commit();
        }
        return $result;
    }

    public function GetUserfollowing($id)
    {
        global $pdo;
        $statament = $pdo->prepare('SELECT COUNT(*) FROM follow WHERE following=:id&&followed!=:id');
        $statament->bindValue(':id', $id);
        $statament->execute();
        return $statament->fetchColumn();
    }

    public function GetUserfollowed($id)
    {
        global $pdo;
        $statament = $pdo->prepare('SELECT COUNT(*) FROM follow WHERE followed=:id&&following!=:id');
        $statament->bindValue(':id', $id);
        $statament->execute();
        return $statament->fetchColumn();
    }

    public function GetUsermoodnum($id)
    {
        global $pdo;
        $statament = $pdo->prepare('SELECT COUNT(*) FROM mood WHERE whos=?');
        $statament->bindValue(1, $id);
        $statament->execute();
        return $statament->fetchColumn();
    }

    public function GetUsermoodsc($id)
    {
        global $pdo;
        $statament = $pdo->prepare('SELECT COUNT(*) FROM mood WHERE whos=?');
        $statament->bindValue(1, $id);
        $statament->execute();
        return $statament->fetchColumn();
    }

    /**
     * get users infomation
     * */
    public static function GetUsersInfo($tableColumn, $extras = '')
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $sql = 'SELECT ' . $tableColumn . ' FROM follow WHERE ' . $extras;
        $statament = $pdo->prepare("{$sql}");
        $statament->execute();
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $result = $statament->fetchAll();
        if (!$inTransaction) {
            $pdo->commit();
        }
        $re = array();
        foreach ($result as $k => $val) {
            foreach ($val as $value) {
                $username = User::GetUsernameById($value);
                $re[$value] = self::GetUserInfo($username)[0];
            }
        }
        JSON::json($re);
    }
}